<%@ page language="java" pageEncoding="UTF-8"%>

<%
	String path = request.getScheme() + "://" +request.getServerName()+":"+request.getServerPort() + request.getContextPath();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <script language="javascript" type="text/javascript" src="<%=path%>/jquery-1.8.3.min.js"></script>
  </head>
  
  <body>
    <table width="95%">
    	<tr>
    		<Td>请求地址:</Td><Td><%=path%><input type="text" name="url" id="url" value="/servlet/Service/" /></Td>
    	<tr>
    	<tr>
    		<Td>请求类型</Td>
    		<Td>
	    		<select id="requestType">
	    			<option value="">------请选择------</option>
	    			<option value="get">get【查询】</option>
	    			<option value="post">post【新增】</option>
	    			<option value="put">put【更新】</option>
	    			<option value="delete">delete【删除】</option>
	    		</select>
    		</Td>
    	<tr>
    	<tr>
    		<Td>鉴权信息</Td>
    		<Td>
	    		<input type="text" name="auth_param" id="auth_param" style="width:700px;"/>
    		</Td>
    	<tr>
    	</tr>
    		<Td>业务入参</Td>
    		<Td>
	    		<textarea name="param" id="param" rows="10" cols="100"></textarea>
    		</Td>
    	<tr>
    </table>
    
    <input type="button" value="发起请求..." style="cursor:hand" onclick="doUrl(url.value,requestType.value)"/>
    <br>
     <br>
    结果集为:<div id='resutDiv'></div>
  </body>
  
  <script type="text/javascript">
  	    var isResult = 1;
    	function do_actions(url,requestType,param)
    	{
    		isResult = 0;
    		$('#center').show();
    		doProgress();
	    	$.ajax({
				type : 'GET',
				url : '<%=path%>/callServlet',
				data : 'requestBean='+param+"&methodType="+requestType+"&retUrl="+url,
				dataType : 'text', 
				cache:false,
				contentType:'application/json',
				success : successfun,// 成功回调方法
				beforeSend : function(XMLHttpRequest) {
					alert("已提交，请稍等...");
				},
				complete : function(XMLHttpRequest, textStatus) {
				},
				error : function(XMLHttpRequest, textStatus, errorThrown){
						isResult = 1;
					 	$('#resutDiv').html(textStatus);
				}
			});
		}
		
		function do_action(url,requestType,param)
    	{
    		isResult = 0;
    		$('#center').show();
    		doProgress();
	    	$.ajax({
				type : 'GET',
				url : '<%=path%>/servlet/FileUpload',
				data : 'userName=我在这里',
				dataType : 'text', 
				cache:false,
				contentType:'application/json',
				success : successfun,// 成功回调方法
				beforeSend : function(XMLHttpRequest) {
					alert("已提交，请稍等...");
				},
				complete : function(XMLHttpRequest, textStatus) {
				},
				error : function(XMLHttpRequest, textStatus, errorThrown){
						isResult = 1;
					 	$('#resutDiv').html(textStatus);
				}
			});
		}
		
		function successfun(data)
		{
			isResult = 1;
			data = decodeURI(data);
			data = data.replaceAll('%3A',':').replaceAll('%2C',',').replaceAll('%3D','=').replaceAll('%2F','/').replaceAll('%3B',';');
			$('#resutDiv').html(data);
		}
		function doUrl(url,requestType)
		{
			var param = '';
			if(url==''){
				alert('请输入目标RMI');
				return false;
			}
			if(requestType==''){
				alert('请选择请求类型');
				return false;
			}
			param = $('#param').val();
			param = param.replace('{','');
			param = param.replace('}','');
			
			param = encodeURI(param);
			var auth_param = $('#auth_param').val();
			auth_param = auth_param.replace('{','');
			auth_param = auth_param.replace('}','');
			if(auth_param == ''){
				param = '{' + param + '}';
			}else{
				param = '{' + auth_param + ',' + param + '}';
			}
			do_action(url,requestType,param);
		}
		
		
		 String.prototype.replaceAll = function (AFindText,ARepText){
			raRegExp = new RegExp(AFindText,"g");
			return this.replace(raRegExp,ARepText);
		 }
		 
		 
		var progress_id = "loading";
		function SetProgress(progress) {
			if (progress) {
				$("#" + progress_id + " > div").css("width", String(progress) + "%"); //控制#loading div宽度
				$("#" + progress_id + " > div").html("请求中，请稍等..."); //显示百分比
			}
		}
		var i = 0;
		function doProgress() {
			if(isResult == 1 && i > 0){
				$('#center').hide();
				i == 0;//结束
				return;
			}
			if (i > 100) {
				//$("#message").html("加载完毕！").fadeIn("slow");//加载完毕提示
				return;
			}
			if (i <= 100) {
				setTimeout("doProgress()", 100);
				SetProgress(i);
				i++;
			}
		}
    </script>
    
    <div id="center" style="display:none">
		<div id="message"></div>
		<div id="loading"><div></div></div>
	</div> 
</html>
